package cm.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: yxh
 * @Description: 比较list不同
 * @Date: Created in 2023/3/16 11:18
 * @Modified By:
 */
public class CompareListUtil {

    /**
     * 比较不同 默认各个list全部已去重
     * @param sourceList
     * @param compareList
     * @return
     */
    public static List<String> findDiff(List<String> sourceList, List<String> compareList) {
        List<String> diff = new ArrayList<>();
        long start = System.currentTimeMillis();
        Map<String, Integer> map = new HashMap<>(compareList.size());
        for (String string : compareList) {
            map.put(string, 1);
        }
        for (String value : sourceList) {
            Integer count = map.get(value);
            if (count != null) {
                map.put(value, ++count);
                continue;
            }
        }
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            if (entry.getValue() == 1) {
                diff.add(entry.getKey());
            }
        }
        System.out.println("方法耗时：" + (System.currentTimeMillis() - start) + " 毫秒");
        return diff;
    }

    /**
     * 比较不同不分主次
     * @param list1
     * @param list2
     * @return
     */
    private static List<String> compareDiff(List<String> list1, List<String> list2) {
        List<String> diff = new ArrayList<>();
        long start = System.currentTimeMillis();
        Map<String, Integer> map = new HashMap<>(list1.size() + list2.size());
        List<String> maxList = list1;
        List<String> minList = list2;
        if (list2.size() > list1.size()) {
            maxList = list2;
            minList = list1;
        }
        for (String string : maxList) {
            map.put(string, 1);
        }
        for (String string : minList) {
            Integer count = map.get(string);
            if (count != null) {
                map.put(string, ++count);
                continue;
            }
            map.put(string, 1);
        }
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            if (entry.getValue() == 1) {
                diff.add(entry.getKey());
            }
        }
        System.out.println("方法耗时：" + (System.currentTimeMillis() - start) + " 毫秒");
        return diff;
    }
}
